﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using FastData;
using FastCtr;
using System.Threading;

namespace WindowsFormsApp2
{
    public partial class Form1 : Form
    {
        NewSQL newSQL = new NewSQL("mysql");
        bool mode = true;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            devGridView1.ParamUnit.Add("Process", "g");
            devGridView1.ParamUnit.Add("Scan_Res", "g");
            Task task = new Task(() =>
              {
                  while (true)
                  {
                      DataTable dataTable;
                      if (mode)
                      {
                          dataTable = newSQL.SqlClient.FindLastLines("cellinformation", 200).Tables[0];
                      }
                      else
                      {
                          dataTable = newSQL.SqlClient.FindLastLines("jiginformation", 30).Tables[0];
                      }

                      if (this.InvokeRequired)
                      {
                          this.BeginInvoke(new Action<DataTable>(t =>
                          {
                              devGridView1.DataTable = t;
                          }), dataTable);
                      }
                      else
                      {
                          devGridView1.DataTable = dataTable;
                      }
                      Thread.Sleep(700);
                  }
              }, TaskCreationOptions.LongRunning);
            task.Start();


            int l = 0;
            Task.Factory.StartNew(() =>
            {
                while (true)
                {
                    l++;
                    Thread.Sleep(1000);
                    newSQL.SqlClient.InsertData($"INSERT INTO `pack_1`.`jiginformation` (`jigcode`) VALUES ('fsed1rrwad{l}')");
                }

            });
        }

        private void timer1_Tick(object sender, EventArgs e)
        {

          

            //if (mode)
            //{

            //    dataTable = newSQL.SqlClient.FindLastLines("cellinformation", 200).Tables[0];
            //    devGridView1.DataTable = dataTable;


            //}
            //else
            //{
            //    dataTable = newSQL.SqlClient.FindLastLines("jiginformation", 2).Tables[0];
            //    devGridView1.DataTable = dataTable;

            //}
        }

        private void menuButton1_SelectedChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form form = new Form();
            form.Controls.Add(newSQL);
            form.ShowDialog();
        }

        DataTable dataTable;
        DataTable dtDND = new DataTable();
        Random random = new Random();

        bool ison = true;
        private void button2_Click(object sender, EventArgs e)
        {
       

            dtDND.Columns.Add("yrr", System.Type.GetType("System.Single"));
            dtDND.Columns.Add("时间", typeof(DateTime));
            dtDND.Columns.Add("age", System.Type.GetType("System.String"));
            dtDND.Columns.Add("age1", System.Type.GetType("System.String"));
            dtDND.Columns.Add("age2", System.Type.GetType("System.String"));


            dtDND.Rows.Add(1f, DateTime.Now, "张三", "123", "1");
            dtDND.Rows.Add(2f, DateTime.Now, "李四", "11223", "2");
            dtDND.Rows.Add(3f, DateTime.Now, "王五", "000", "3");
            dtDND.Rows.Add(8f, DateTime.Now, "王五", "000", "7");
            float m = 5.2f;
            devGridView1.DataTable = dtDND;
            Task.Factory.StartNew(() =>
            {
                while (ison)
                {
                    Thread.Sleep(1000);
                    this.Invoke(new Action(() =>
                    {
                        int k = random.Next(0, 1000);
                        DataRow dataRow = dtDND.NewRow();
                        dataRow.ItemArray = new object[] { m, DateTime.Now, "ERR", k.ToString(), "" };
                        dtDND.Rows.InsertAt(dataRow, 0);
                        m++;
                        if (dtDND.Rows.Count > 30)
                        {
                            dtDND.Rows.RemoveAt(dtDND.Rows.Count - 1);
                        }
                        devGridView1.DataTable = dtDND;
                    }));
                }
            });

        }

        private void button3_Click(object sender, EventArgs e)
        {
            ison = false;
            //dataTable = newSQL.SqlClient.FindLastLines("jiginformation", 2).Tables[0];
            //devGridView1.gridControl1.DataSource = dataTable;
            //devGridView1.gridControl1.RefreshDataSource();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            mode = !mode;
        }

        private void button5_Click(object sender, EventArgs e)
        {
            devGridView1.RowClick += DevGridView1_RowClick1;
        }

        private void DevGridView1_RowClick1(DataRow dataRow, MouseEventArgs e)
        {
            
        }

    }
}
